<template>
  <div>
    <PathThink2
      :pathConfig="pathConfig"
      :leftNodeLists="leftNodeLists"
      :centerNodeLists="centerNodeLists"
      :rightNodeLists="rightNodeLists"/>
  </div>
</template>

<script>
import PathThink2 from "@/components/PathThink2"

export default {
  components: {
    PathThink2,
  },
  data() {
    return {
      id: '',
      pathConfig: {
        id: '',
        name: '',
        type: 1
      },
      leftNodeLists: [],
      centerNodeLists: [],
      rightNodeLists: [],
      nameList: [
        "原材料获取",
        "生产制造",
        "分销",
        "使用",
        "废弃",
      ],
      LeftNameList: ['原材料/物料', "耗材", "能源", "资源", "运输/通勤", "固碳", "包装材料"]
    }
  },
  mounted() {
    console.log(this.$route.query)
    if (this.$route.query.id) {
      // 初始化模型元素
      this.id = this.$route.query.id
      this.initPath()
    }
  },
  methods: {
    // 设置初始信息
    initPath() {
      this.$API.product.getCalculationDetail({
        id: this.id
      }).then(response => {
        var data = response.data
        this.pathConfig.name = data.calculationModelDO.name
        this.pathConfig.type = data.calculationModelDO.lifecycle ? 1 : 0
        this.pathConfig.id = this.id
        this.initItem(data.lists)
      })
    },
    // 初始化工序信息
    initItem(li) {
      // 初始化列表
      if (this.pathConfig.type == 1) {
        this.leftNodeLists = [[], [], [], [], []]
        this.rightNodeLists = [[], [], [], [], []]
        this.centerNodeLists = [[], [], [], [], []]
      } else {
        this.leftNodeLists = [[], [], [], []]
        this.rightNodeLists = [[], [], [], []]
        this.centerNodeLists = [[], [], [], []]
      }
      // 解构填入结点
      for (var i = 0; i < li.length; i++) {
        var typeIndex = this.nameList.indexOf(li[i].processPMResVO.periodName)

        this.leftNodeLists[typeIndex].push([])
        this.rightNodeLists[typeIndex].push([])
        this.centerNodeLists[typeIndex].push(li[i].processPMResVO)
        var processIndex = this.leftNodeLists[typeIndex].length - 1;

        for (var j = 0; j < li[i].emissions.length; j++) {
          if (this.LeftNameList.indexOf(li[i].emissions[j].type) != -1) {
            var has = false
            for (var k = 0; k < this.leftNodeLists[typeIndex][processIndex].length; k++) {
              if (this.leftNodeLists[typeIndex][processIndex][k].name == li[i].emissions[j].name) {
                has = true;
                break
              }
            }
            if (!has) {
              this.leftNodeLists[typeIndex][processIndex].push({
                ...li[i].emissions[j],
                processId: parseInt(li[i].processPMResVO.id),
                calculationId: parseInt(this.id),
                dan: li[i].emissions[j].emissionsUnitN + '/' + li[i].emissions[j].emissionsUnitD,
                emissionId: li[i].emissions[j].id
              })
            }


          } else {
            var has = false
            for (var k = 0; k < this.rightNodeLists[typeIndex][processIndex].length; k++) {
              if (this.rightNodeLists[typeIndex][processIndex][k].name == li[i].emissions[j].name) {
                has = true;
                break
              }
            }
            if (!has) {
              this.rightNodeLists[typeIndex][processIndex].push({
                ...li[i].emissions[j],
                processId: parseInt(li[i].processPMResVO.id),
                calculationId: parseInt(this.id),
                dan: li[i].emissions[j].emissionsUnitN + '/' + li[i].emissions[j].emissionsUnitD,
                emissionId: li[i].emissions[j].id
              })
            }
          }
        }
      }
      console.log("result:")
      console.log(this.leftNodeLists)
      console.log(this.centerNodeLists)
      console.log(this.rightNodeLists)

    }

  }
}
</script>

<style>

</style>
